<template>
    <div class="home" v-cloak>
        <div class="userInfo panel">
            <div class="photo">
                <img :src="customer.avatar">
            </div>
            <div class="name fs16">{{customer.name}}</div>
        </div>
        <div class="panel">
            <mt-cell>
                <div slot="title">
                    <div class="mt-cols menu">
                        <div class="mt-col-1">
                            <router-link to="/config"><img src="../img/icon_sz.png"><br>课程设置</router-link>
                        </div>
                        <div class="mt-col-1">
                            <a @click="previewCourse"><img src="../img/icon_yl.png"><br>课程预览</a>
                            <!-- <router-link to="/preview"><img src="../img/icon_yl.png"><br>课程预览</router-link> -->
                        </div>
                        <div class="mt-col-1">
                            <a @click="shareCourse"><img src="../img/icon_zf.png"><br>邀请学员</a>
                        </div>
                    </div>
                </div>
            </mt-cell>
        </div>
        <div class="panel student">
            <mt-cell class="tit" title="学员管理" to="/students" is-link>
                <span>查看全部</span>
            </mt-cell>
            <mt-cell class="ctx">
                <div slot="title">
                    <div class="mt-cols">
                        <div class="mt-col-1">
                            <div class="count">{{totalCustomerCount}}</div>
                            <div>总学员数</div>
                        </div>
                        <div class="mt-col-1">
                            <div class="count">{{newCustomerCount}}</div>
                            <div>今日新增数</div>
                        </div>
                    </div>
                </div>
            </mt-cell>
        </div>
        <div class="footer">
            时光企鹅 提供技术支持
        </div>
    </div>
</template>

<script>
    import { getSummary, getCourseConf } from '@/api'
    export default {
        name: 'home',
        data() {
            return {
                holderId: 0,
                customer: {
                    avatar: '',
                    name: ''
                },
                totalCustomerCount: 0,
                newCustomerCount: 0,
                courseConfig: false
            }
        },
        created(){
            getSummary().then( res => {
                let {headImageUrl, nickname, totalCustomerCount, newCustomerCount, holderId} = res.data.result
                this.customer.avatar = headImageUrl
                this.customer.name = nickname
                this.newCustomerCount = newCustomerCount
                this.totalCustomerCount = totalCustomerCount
                this.holderId = holderId
            })
            getCourseConf().then( res=> {
                if(res.data.result && res.data.result.name){
                    this.courseConfig = true
                }
            })
        },
        methods: {
            previewCourse() {
                if(this.courseConfig){
                    this.$router.push('/preview')
                } else {
                    this.$message.confirm('您还没有设置课程，请先进行课程设置').then(() => {
                        this.$router.push('/config')
                    });
                }
            },
            shareCourse() {
                if(this.courseConfig){
                    window.location.href='visitor.html#/preview?self=1&hId='+this.holderId
                } else {
                    this.$message.confirm('您还没有设置课程，请先进行课程设置').then(() => {
                        this.$router.push('/config')
                    });
                }
            },
        },
    }
</script>

<style lang="scss" scoped>
    .home {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background-color: var(--deepBack);

        /deep/ {
            .userInfo {
                text-align: center;
                padding: .25rem 0;

                .photo {
                    width: .6rem;
                    height: .6rem;
                    border-radius: 50%;
                    overflow: hidden;
                    // background-color: #ddd;
                    display: inline-block;

                    img {
                        width: .8rem;
                        height: .8rem;
                    }
                }

                .name {
                    padding: .06rem 0 0;
                }
            }

            .mt-col-1 {
                text-align: center;
            }

            .menu {
                padding: .3rem .2rem;

                img {
                    width: .55rem;
                    padding-bottom: .1rem;
                }

                a {
                    text-decoration: none;
                    font-size: 14px;
                    color: var(--textColor);
                }
            }

            .student {
                .tit {
                    .mint-cell-title {
                        text-align: center;
                        font-weight: bold;
                    }

                    .mint-cell-value.is-link {
                        position: absolute;
                        right: 6px;
                        top: 18px;
                        font-size: 12px;
                    }
                }
                .ctx{
                    .mt-cols{
                        padding: .3rem .2rem .4rem;
                        font-size: 14px;
                    }
                    .count{
                        font-size: 32px;
                        font-weight: bold;
                        line-height: 1.1;
                        color: #000;
                        padding-bottom: .07rem;
                    }
                }
            }

            .mint-cell:first-child .mint-cell-wrapper,
            .mint-cell:last-child {
                background: none;
            }
        }
        .footer{
            position: absolute;
            width: 100%;
            text-align: center;
            bottom: 20px;
            font-size: 12px;
            color: #ccc;
        }
    }
</style>